function Food(m) {
    this.x = 0;
    this.y = 0;
    this.div = document.createElement("div");
    this.div.className = "food";
    this.map = m;
    this.map.appendChild(this.div)
 }
 
 Food.prototype.randomLoaction = function () {
    var maxX = 900 / 20 - 1;
    var maxY = 600 / 20 - 1;
    var indexX = getIntNum(0, maxX);
    var indexY = getIntNum(0, maxY);
    this.x = indexX * 20;
    this.y = indexY * 20;
    this.div.style.left = this.x + "px";
    this.div.style.top = this.y + "px"
 };
 
 function getIntNum(min, max) {
    var num = parseInt(Math.random() * (max - min + 1) + min);
    return num
 };
 
 function Snake(m, f) {
    this.direction = "right";
    this.bodys = [];
    this.map = m;
    this.food = f;
    this.createBodys()
 }
 
 Snake.prototype.createBodys = function () {
    for (var i = 0; i < 3; i++) {
        this.insertNewHead()
    }
 };
 Snake.prototype.insertNewHead = function () {
    var newHead = document.createElement("div");
    newHead.className = "snake-head";
    var location = this.getNewHeadLoaction();
    newHead.style.left = location.left + "px";
    newHead.style.top = location.top + "px";
    this.map.appendChild(newHead);
    var oldHead = this.bodys[0];
    if (oldHead != undefined) {
        oldHead.className = "snake-body"
    }
    this.bodys.unshift(newHead)
 };
 Snake.prototype.getNewHeadLoaction = function () {
    var x = 0;
    y = 0;
    var oldHead = this.bodys[0];
    if (oldHead == undefined) {
        return {left: x, top: y}
    }
    x = oldHead.offsetLeft;
    y = oldHead.offsetTop;
    switch (this.direction) {
        case"left":
            x = x - 20;
            break;
        case"right":
            x = x + 20;
            break;
        case"bottom":
            y = y + 20;
            break;
        case"top":
            y = y - 20;
            break
    }
    return {left: x, top: y}
 };
 Snake.prototype.move = function () {
    var obj = this.getNewHeadLoaction();
    if (obj.left < 0 || obj.left == 900 || obj.top < 0 || obj.top == 600) {
        alert("想不开死了");
        return true
    }
    var last = this.bodys.pop();
    last.className = "snake-head";
    var oldHead = this.bodys[0];
    oldHead.className = "snake-body";
    this.bodys.unshift(last);
    last.style.left = obj.left + "px";
    last.style.top = obj.top + "px";
    if (obj.left == this.food.x && obj.top == this.food.y) {
        this.insertNewHead();
        this.food.randomLoaction()
    }
    return false
 };
 
 function Game(m) {
    this.food = new Food(m);
    this.snake = new Snake(m, this.food)
 }
 
 Game.prototype.start = function () {
    this.food.randomLoaction();
    var snake = this.snake;
    var flag = window.setInterval(function () {
        var isDead = snake.move();
        console.log(isDead);
        if (isDead) {
            clearInterval(flag)
        }
    }, 100);
    document.onkeydown = function (e) {
        var code = e.keyCode;
        switch (code) {
            case 37:
                if (snake.direction != "right") {
                    snake.direction = "left"
                }
                break;
            case 38:
                snake.direction = "top";
                break;
            case 39:
                snake.direction = "right";
                break;
            case 40:
                snake.direction = "bottom";
                break
        }
    }
 };
 
